angular.module('meMyLevelcontrollers', [])

  .controller('MeMyLevelCtrl', function ($scope,$ionicActionSheet, $state, $timeout, Storage, MeMyLevel, ENV,History) {


    var memberId;

    $scope.ENV = ENV;

    $scope.pageInfo = {
      member_img_url : "",
      member_point_count_history : 0,
      mr_name : "",
      mr_img_url : "",
      mr_max_point : 0,
      mr_id : 0
    };

    $scope.appendNextLine = function(text){
        var firstText = text.substring(0,2);
        var lastText = text.substring(2);
        return firstText+'<br/>'+lastText;
    }
    //进入我的等级界面时，进行数据加载
    $scope.$on('$ionicView.beforeEnter', function () {
      memberId = Storage.get("member").member_id;
      console.log(memberId);
      MeMyLevel.getMyLevelPageInfo(memberId);
    });


    $scope.$on('Me.myLevelInfoGot',function(){
      var result = MeMyLevel.getMyLevelPageInfoJson();
      $scope.pageInfo.member_img_url = result.member_img_url;
      $scope.pageInfo.member_point_count_history = result.member_point_count_history;
      $scope.pageInfo.mr_name = result.mr_name;
      $scope.pageInfo.mr_img_url = result.mr_img_url;
      $scope.pageInfo.mr_max_point = result.mr_max_point;
      $scope.pageInfo.mr_id = result.mr_id;//会员的等级

      $scope.rankList = result.rankList;

      var position;
      $scope.rankBar = [];
      //处理等级排名栏
      for(var i = 0;i< $scope.rankList.length;i++){
        //判断会员当前等级所在的位置
        if($scope.rankList[i].mr_id == $scope.pageInfo.mr_id){
          position = i;

        }
      }
      console.log($scope.rankList[position+1]);
      console.log($scope.rankList[position+2]);
      $scope.rankBar.push($scope.rankList[position+1]);
      $scope.rankBar.push($scope.rankList[position+2]);

    });

    $scope.goBack = function () {
      console.log("test");
      History.goback();
    };
    //改变数字的显示，添加逗号分割
    $scope.getFormatNum = function (num) {
      return formatNum(num);
    };


  });
